package com.novel.controller;

import com.novel.annotation.ResponseResult;
import com.novel.entiy.Chapter;
import com.novel.entiy.Classify;
import com.novel.entiy.Novel;
import com.novel.entiy.dto.*;
import com.novel.entiy.vo.*;
import com.novel.service.NovelService;
import com.novel.utils.CommonUtil;
import com.novel.utils.PageResult;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chihuo
 * @date 2022/4/5
 */
@ResponseResult
@RestController
@RequestMapping("/novel")
@RequiredArgsConstructor
public class NovelController {

    private final NovelService novelService;


    @ApiOperation("新建小说")
    @PostMapping("/addNovel")
    public void addNovel(@Validated @RequestBody NovelDTO novelDTO, BindingResult bindingResult) {
        CommonUtil.paramValid(bindingResult);
        novelService.addNovel(novelDTO);
    }

    @ApiOperation("获取分类列表")
    @GetMapping("/getAllClassify")
    public List<Classify> getAllClassify() {
        return novelService.getAllClassify();
    }

    @ApiOperation("获取小说列表")
    @PostMapping("/getNovelList")
    public PageResult<Novel> getNovelList(@RequestBody NovelPageDTO novelPageDTO) {
        return novelService.getNovelList(novelPageDTO);
    }

    @ApiOperation("获取小说详情")
    @GetMapping("/getNovelInfo/{id}")
    public NovelVO getNovelInfo(@PathVariable Long id) {
        return novelService.getNovelInfo(id);
    }

    @ApiOperation("编辑小说")
    @PostMapping("/editNovel")
    public void editNovel(@Validated @RequestBody NovelDTO novelDTO, BindingResult bindingResult) {
        CommonUtil.paramValid(bindingResult);
        novelService.editNovel(novelDTO);
    }

    @ApiOperation("删除小说")
    @GetMapping("/delNovel/{id}")
    public void delNovel(@PathVariable Long id) {
        novelService.delNovel(id);
    }

    @ApiOperation("新增章节———可以直接发布，也可以保存草稿，传对应的状态值")
    @PostMapping("/addChapter")
    public void addChapter(@Validated @RequestBody ChapterDTO chapterDTO, BindingResult bindingResult) {
        CommonUtil.paramValid(bindingResult);
        novelService.addChapter(chapterDTO);
    }

    @ApiOperation("发布章节——草稿状态的发布")
    @GetMapping("/publishChapter/{id}")
    public void publishChapter(@PathVariable Long id) {
        novelService.publishChapter(id);
    }

    @ApiOperation("更新章节")
    @PostMapping("/editChapter")
    public void editChapter(@Validated @RequestBody ChapterDTO chapterDTO, BindingResult bindingResult) {
        CommonUtil.paramValid(bindingResult);
        novelService.editChapter(chapterDTO);
    }

    @ApiOperation("阅读小说章节")
    @PostMapping("/read")
    public ReadVO read(@RequestBody ReadDTO readDTO) {
        return novelService.read(readDTO);
    }

    @ApiOperation("增加小说阅读量")
    @PostMapping("/readNum")
    public void readNum(@RequestBody ReadDTO readDTO) {
        novelService.readNum(readDTO);
    }

    @ApiOperation("追书")
    @GetMapping("/subscribe/{novelId}")
    public void subscribe(@PathVariable Long novelId) {
        novelService.subscribe(novelId);
    }

    @ApiOperation("取消追书")
    @GetMapping("/cancelSubscribe/{novelId}")
    public void cancelSubscribe(@PathVariable Long novelId) {
        novelService.cancelSubscribe(novelId);
    }

    @ApiOperation("评分")
    @GetMapping("/score/{novelId}/{score}")
    public void score(@PathVariable Long novelId, @PathVariable String score) {
        novelService.score(novelId, score);
    }

    @ApiOperation("获取小说所有章节草稿")
    @GetMapping("/getAllChapterDraft/{novelId}")
    public List<ChapterVO> getAllChapterDraft(@PathVariable Long novelId) {
        return novelService.getAllChapterDraft(novelId);
    }

    @ApiOperation("获取小说所有章节")
    @GetMapping("/getAllChapter/{novelId}/{sort}")
    public List<ChapterVO> getAllChapter(@PathVariable Long novelId, @PathVariable Integer sort) {
        return novelService.getAllChapter(novelId, sort);
    }

    @ApiOperation("我的小说-作者")
    @PostMapping("/getMyBookList")
    public PageResult<Novel> getMyBookList(@RequestBody PageParams pageParams) {
        return novelService.getMyBookList(pageParams);
    }

    @ApiOperation("我的书架")
    @PostMapping("/getMyBookSelf")
    public PageResult<BookSelfVO> getMyBookSelf(@RequestBody PageParams pageParams) {
        return novelService.getMyBookSelf(pageParams);
    }

    @ApiOperation("判小说断是否存在草稿")
    @GetMapping("/existDraft/{novelId}")
    public boolean existDraft(@PathVariable Long novelId) {
        return novelService.existDraft(novelId);
    }

    @ApiOperation("章节列表-分页")
    @PostMapping("/chapterList")
    public PageResult<ChapterVO> chapterList(@RequestBody ChapterPageDTO chapterPageDTO) {
        return novelService.chapterList(chapterPageDTO);
    }

    @ApiOperation("获取章节内容")
    @GetMapping("/getChapter/{chapterId}")
    public Chapter getChapter(@PathVariable Long chapterId) {
        return novelService.getChapter(chapterId);
    }

    @ApiOperation("设置小说收费章节")
    @PostMapping("/setCharge")
    public void setCharge(@RequestBody ChargeDTO charge) {
        novelService.setCharge(charge);
    }

    @ApiOperation("搜索")
    @GetMapping("/search/{query}")
    public List<SearchVO> search(@PathVariable String query) {
        return novelService.search(query);
    }

    @ApiOperation("完结小说")
    @GetMapping("/finish/{novelId}")
    public void finish(@PathVariable Long novelId){
        novelService.finish(novelId);
    }


}
